package com.viphuli.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import butterknife.Bind;

import com.umeng.update.UmengUpdateAgent;
import com.viphuli.app.R;
import com.viphuli.common.DoubleClickExitHelper;
import com.viphuli.common.MainTabEnum;
import com.viphuli.util.MapUtils;

/**
 * 主框架
 * 
 * @author li.li
 *
 */
public class MainFragment extends BaseProgressFragment implements OnTabChangeListener {

	@Bind(R.id.id_main_tabhost)
	protected FragmentTabHost tabHost;

	private MainTabEnum[] tabs = MainTabEnum.values();

	private DoubleClickExitHelper mDoubleClickExit;

	private int curTabIndex;

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//友盟更新
		UmengUpdateAgent.update(getActivity());
	}

	@Override
	protected int obtainContentRes() {
		return R.layout.act_main;
	}

	@Override
	protected void initParams() {
		Bundle args = getArguments();
		curTabIndex = args.getInt("tab_index", MainTabEnum.home.getIndex());
	}

	@Override
	protected void initView() {
		mDoubleClickExit = new DoubleClickExitHelper(getActivity());

		initTabs();
	}

	@Override
	protected void initData() {

		//定位城市
		MapUtils.locate(getActivity(), caller.getLeftTitle());

	}

	@Override
	public void onBackPressed() {
		// 是否退出应用
		mDoubleClickExit.onBackPressed();
	}

	/**
	 * 初始化tabs
	 */
	private void initTabs() {
		tabHost.setup(caller, getChildFragmentManager(), R.id.id_main_content);

		for (int i = 0; i < tabs.length; i++) {

			MainTabEnum tab = tabs[i];
			// 给每个Tab按钮设置图标、文字和内容
			String name = tab.getName();
			TabSpec tabSpec = tabHost.newTabSpec(name).setIndicator(getTabItemView(tab));
			// 将Tab按钮添加进Tab选项卡中
			tabHost.addTab(tabSpec, tab.getFrag(), null);
			// 设置Tab按钮的背景
			tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.bg_main_bottom_tab);

		}

		//tab切换监听
		tabHost.setOnTabChangedListener(this);

		//默认选择
		tabHost.setCurrentTab(curTabIndex);

	}

	/**
	*
	* 给每个Tab按钮设置图标和文字
	*/
	private View getTabItemView(MainTabEnum tab) {
		View view = LayoutInflater.from(caller).inflate(R.layout.tab_item_view, null);
		TextView imageView = (TextView) view.findViewById(R.id.id_main_bottom_tab_icon);
		imageView.setBackgroundResource(tab.getIcon());
		TextView textView = (TextView) view.findViewById(R.id.id_main_bottom_tab_text);
		String name = tab.getName();
		textView.setText(name);

		return view;
	}

	@Override
	public void onTabChanged(String tabId) {
		
		if(MainTabEnum.home.getName().equals(tabId))
			caller.getLeftTitle().setVisibility(View.VISIBLE);
		else
			caller.getLeftTitle().setVisibility(View.GONE);

	}

}
